<?php
header('Content-Type: text/html; charset=utf-8');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../libs/phpMailer/class.phpmailer.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';

session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = MiscUtils::getParam('action', '');
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'remove_addr':
      remove_addr($myPdo);
      break;
    case 'save_addr':
      save_addr($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function remove_addr($myPdo) {
  $id = MiscUtils::getParam('id', 0);
  $is_default = MiscUtils::getParam('is_default', 0);
  $customer_id = MiscUtils::getParam('customer_id', 0);
  $to = MiscUtils::getParam('to', '');
  
  $cond_vals = new stdClass(); 
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $id);
  DbUtils::deleteByCond($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals);
  
  if ($is_default == 1) {
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.customer_id = :v1';
    $cond_vals->v = array(':v1' => $customer_id);
    $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) > 0) {
      $tmp[0]->is_default = 1;
      DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $tmp[0]);
    }
  }
  
  echo "
  <script>
  location.href = '$to';
  </script>
  ";
  return;
}

function save_addr($myPdo) {
  $to = MiscUtils::getParam('to', '');
  $obj = new stdClass();
  $obj->id = MiscUtils::getParam('id', 0);
  $obj->first_name = MiscUtils::getParam('first_name', '');
  $obj->last_name = MiscUtils::getParam('last_name', '');
  $obj->address1 = MiscUtils::getParam('address1', '');
  $obj->address2 = MiscUtils::getParam('address2', '');
  $obj->city = MiscUtils::getParam('city', '');
  $obj->country = MiscUtils::getParam('country', '');
  $obj->zip = MiscUtils::getParam('zip', '');
  $obj->phone = MiscUtils::getParam('phone', '');
  $obj->email = MiscUtils::getParam('email', '');
  $obj->_idx = MiscUtils::getParam('idx', '');
  
  unset($_SESSION['addr_idx']);
  unset($_SESSION['first_name_err']);
  unset($_SESSION['last_name_err']);
  unset($_SESSION['address1_err']);
  unset($_SESSION['address2_err']);
  unset($_SESSION['city_err']);
  unset($_SESSION['country_err']);
  unset($_SESSION['zip_err']);
  unset($_SESSION['phone_err']);
  unset($_SESSION['email_err']);
  unset($_SESSION['first_name']);
  unset($_SESSION['last_name']);
  unset($_SESSION['address1']);
  unset($_SESSION['address2']);
  unset($_SESSION['city']);
  unset($_SESSION['country']);
  unset($_SESSION['zip']);
  unset($_SESSION['phone']);
  unset($_SESSION['email']);
  
  $err = 0;
  if ($obj->first_name == '') {
    $_SESSION['first_name_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->last_name == '') {
    $_SESSION['last_name_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->address1 == '') {
    $_SESSION['address1_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->city == '') {
    $_SESSION['city_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->country == '') {
    $_SESSION['country_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->phone == '') {
    $_SESSION['phone_err'] = 'Can not be empty';
    $err = 1;
  }
  if ($obj->email == '') {
    $_SESSION['email_err'] = 'Can not be empty';
    $err = 1;
  }
  if (!filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
    $_SESSION['email_err'] = 'Not a proper email';
    $err = 1;
  }
  
  if ($err == 1) {
    $_SESSION['addr_idx'] = $obj->_idx;
    $_SESSION['first_name'] = $obj->first_name;
    $_SESSION['last_name'] = $obj->last_name;
    $_SESSION['address1'] = $obj->address1;
    $_SESSION['address2'] = $obj->address2;
    $_SESSION['city'] = $obj->city;
    $_SESSION['country'] = $obj->country;
    $_SESSION['zip'] = $obj->zip;
    $_SESSION['phone'] = $obj->phone;
    $_SESSION['email'] = $obj->email;
    echo "
    <script>
    location.href = '$to';
    </script>
    ";
    return;
  }
  if ($obj->_idx != -1) {
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $obj);
  } else {
    unset($obj->id);
    $obj->customer_id = $_SESSION['index' . USER]->id;
    $obj->is_default = 0;
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.customer_id = :v1';
    $cond_vals->v = array(':v1' => $obj->customer_id);
    $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) == 0) {
      $obj->is_default = 1;
    }
    
    DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $obj);
  }
  echo "
  <script>
  location.href = '$to';
  </script>
  ";
  return;
}
?>
